package com.hsmpay.yl8583.service;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import com.hsmpay.yl8583.utils.Field;
import com.hsmpay.yl8583.utils.Package64;
import com.hsmpay.yl8583.utils.PackageUtil;
import com.hsmpay.yl8583.utils.SpringUtil;
import com.hsmpay.yl8583.utils.TestDes;
import com.hsmpay.utils.DesUtil;
import com.hsmpay.utils.WriteFile;
import com.hsmpay.utils.system.ConConfig;


public class RuiShengGTSocketToBoss {
	private ApplicationContext act;
	static WriteFile wf = new WriteFile("ds_RuiSheng","guotong");
	public static String debug = ConConfig.getAttribute("RUISHENG_GT_DAISHOU_DEBUG_STATUS");
	
	static String ipAdd="112.5.145.88";
	static String IPort="9906";
	String itimeout="60000";
	
	String ShopCode = "Z07000000141863";//商户号
	String PosNo = "30164177";			//终端号
	
	String tpdu = "6000030000";
	String header = "603100310001";
	String batch = "000013"; 			 // 批次号
	String serialNumber = "900009"; 	 // 系统跟踪号(POS中心流水号)
	String operator="001";				 //操作员

	
	//封顶密文密码密钥
	String PinKey="B28EC2E3F8715F5D789565AC845627AE";
	//封顶密文报文密钥
	String MacKey="B28EC2E3F8715F5DB28EC2E3F8715F5D";
	//主密钥
	String MasterKey="74757105913910210314678310910894";
	
	
	byte[] bitmap = new byte[8];

	static{
		if("1".equals(debug)){
			ipAdd="112.5.192.232";//国通  112.5.192.232  9443
			IPort="9443";
		}
	}
	
	/**
	 * 构造函数
	 * @param ShopCode
	 * @param PosNo
	 * @param batch
	 * @param MasterKey
	 * @param PinKey
	 * @param MacKey
	 */
	public RuiShengGTSocketToBoss(String ShopCode,String PosNo,String batch,String MasterKey,String PinKey,String MacKey){
		this.ShopCode = ShopCode;
		this.PosNo = PosNo;
		
		this.batch = batch;
		this.MasterKey = MasterKey;
		this.PinKey = PinKey;
		this.MacKey = MacKey;
		if(StringUtils.isEmpty(batch.trim())){
			this.batch = "000001";
		}
		
		act = SpringUtil.getApplicationContext();//new ClassPathXmlApplicationContext("anxinybConfig.xml");
		wf.log("tpdu:" + this.tpdu, "调试");
		wf.log("ShopCode(商户号):" + this.ShopCode, "调试");
		wf.log("PosNo(终端号):" + this.PosNo, "调试");
		wf.log("batch(批次号):" + this.batch, "调试");
		wf.log("MasterKey:" + this.MasterKey, "调试");
		wf.log("PinKey:" + this.PinKey, "调试");
		wf.log("MacKey:" + this.MacKey, "调试");
	}

	/**
	 * 通信检测(0000)
	 * 
	 * @return 是否正常
	 */
	public String detection(byte[] sendStr) {
		SendSocket send = new SendSocket(ipAdd, IPort, itimeout,wf);
		return send.detection(sendStr);
	}

	public String AddSerialNumber(String SerialNumber) {
		int OldLen = 6;
		String NewStr = (Integer.parseInt(SerialNumber) + 1) + "";
		int NewLen = NewStr.length();
		if (NewLen == 7) {
			NewStr = "000000";
			NewLen = 6;
		}
		for (int i = 0; i < (OldLen - NewLen); i++) {
			NewStr = "0" + NewStr;
		}
		return NewStr;
	}

	public String unAddSerialNumber(String SerialNumber) {
		int OldLen = 6;
		String NewStr = "";
		if (SerialNumber.equals("000000")) {
			NewStr = "999999";
		} else {
			NewStr = (Integer.parseInt(SerialNumber) - 1) + "";
		}
		int NewLen = NewStr.length();
		for (int i = 0; i < (OldLen - NewLen); i++) {
			NewStr = "0" + NewStr;
		}
		return NewStr;
	}

	// 将字符串按指定长度返回,不足位数在前面加0。
	public String GetNewStr(String str, int len) {
		String NewStr = "";
		for (int i = 0; i < len - str.length(); i++) {
			NewStr += "0";
		}
		NewStr += str;
		return NewStr;
	}
	//获取字符串字节长度(双字节长度为2,单字节长度为1)
	public int strlen(String str){ 
		int len=0;
	    try {
			len= (new String(str.getBytes("GBK"), "ISO-8859-1")).length();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		return len;
	}
	//获取字符串中单双字节的个数并以";"分隔返回。
	public String getCharString(String inputText)
	{
		int count = 0; 
		int chCharCount = 0;
		int dconut=0;
		char[] charArray = inputText.toCharArray(); 
		for (int i = 0; i < charArray.length; i++) { 
			char cn = charArray[i]; 
			byte[] bytes = (String.valueOf(cn)).getBytes(); 
			if (bytes.length == 1) { //单字节字符
				dconut++;
				//英文字符 
				//int ascii = bytes[0]; 
			}else if (bytes.length == 2) { //双字节字符 
				count++; 
				int hightByte = 256 + bytes[0]; 
				int lowByte = 256 + bytes[1]; 
				int ascii = (256 * hightByte + lowByte) - 256 * 256; 
				if (ascii >= -20319 && ascii < -10247) { //中文字符 
					chCharCount++; 
				} 
			} 
		} 
		System.out.println( "共有 " + dconut + " 个单字节字符! "); 
		System.out.println( "共有 " + count + " 个双字节字符! "); 
		System.out.println( "共有 " + chCharCount + " 汉字! "); 
		return dconut+";"+count;
	}
	/**
	 * 终端签到
	 * 
	 * @return 批次号
	 * @throws Exception
	 */
	public String Login0800(String serialNumber) throws Exception {
		String LoginMsg = "0";
		String TYPE_ID = "0800";
		wf.log("-------------------请求包域值信息-------------------",	"调试");
		Package64 pk = new Package64();
		pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
		pk.setHeader(PackageUtil.hexStringToByte(this.header));
		pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
		wf.log("[tpdu]=" + this.tpdu, "调试");
		wf.log("[header]=" + this.header, "调试");
		wf.log("[messagetype]=" + TYPE_ID, "调试");
		// 11、41、42、60、63
		String str = "bit11,bit41,bit42,bit60,bit63";
		for (String one : str.split(",")) {
			Field filed = (Field) act.getBean(one);
			if(one.equals("bit11"))
			{
				filed.valueof(serialNumber);
			}else if (one.equals("bit32")) {
				filed.valueof("00000000001");
			}else if (one.equals("bit41")) {
				filed.valueof(this.PosNo);
			}else if (one.equals("bit42")) {
				filed.valueof(this.ShopCode);
			} else if (one.equals("bit60")) {
				filed.valueof("00" + this.batch + "003");
			} else if (one.equals("bit63")) {
				filed.valueof(PackageUtil.getCnASCII("0"+this.operator));
			}
			pk.getData().put(filed.getFiledNumber(), filed);
		}
		this.bitmap = Package64.CreateBitMap(pk.getData().keySet());
		wf.log("[bitmap]=" + PackageUtil.byte2hex(this.bitmap),"调试");
		for (Integer one : pk.getData().keySet()) {
			wf.log("bit" + one + ":"+ PackageUtil.byte2hex(pk.getData().get(one).toByte()),"调试");
		}
		wf.log("----------------------------------------------------", "调试");
		pk.setBitmap(this.bitmap);
		byte[] pk_byteArray = pk.package64();
		ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
		buff.putShort((short) pk_byteArray.length);
		buff.put(pk_byteArray);


		wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "	+ PackageUtil.byte2hex(buff.array()), "调试");
		wf.log("", "调试");
		LoginMsg = detection(buff.array());
		wf.log("响应包:长度 " + LoginMsg.length() / 2 + " 数据 "+ LoginMsg, "调试");
		if ((!LoginMsg.equals("-1")) && (!LoginMsg.equals(""))) {
			LoginMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(LoginMsg), "39,60,62",wf);
			if(LoginMsg.length()>50){
				String startMsg = LoginMsg.substring(0, LoginMsg.lastIndexOf("_"));
				String bit62 = LoginMsg.substring(LoginMsg.lastIndexOf("_")+1);
				bit62 = bit62.substring(0, 32)+"_"+bit62.substring(40,56)+bit62.substring(40,56);
				LoginMsg = startMsg+"_"+bit62;
			}
		} else {
			return "-1_";
		}
		wf.log("银联签到返回数据:" + LoginMsg, "调试");
		return LoginMsg;
	}
	
	public String Query(String trk2, String trk3, String pin,String accountNumber,String serialNumber,String bit23,String bit55,String bit14)throws Exception {
		String des_pin=TestDes.MakeDesPin(accountNumber, pin, PinKey,MasterKey);
		
		String QueryMsg = "";
		String TYPE_ID = "0200";
		wf.log("-------------------请求包域值信息-------------------","调试");
		Package64 pk = new Package64();
		pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
		pk.setHeader(PackageUtil.hexStringToByte(this.header));
		pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
		wf.log("[tpdu]=" + this.tpdu, "调试");
		wf.log("[header]=" + this.header, "调试");
		wf.log("[messagetype]=" + TYPE_ID, "调试");
		// 3、11、22、25、26、35、36、41、42、49、52、53、60、64
		String str = "bit3,bit11,bit22,bit25,bit26,bit35,bit36,bit41,bit42,bit49,bit52,bit53,bit60,bit64";
		String temp52=",bit52";
		String temp35=",bit35";
		String temp36=",bit36";
		
		String temp23=",bit23";
		String temp55=",bit55";
		String temp14=",bit14";
		if("".equals(trk2.trim()))
		{
			temp35="";
		}
		if("".equals(trk3.trim()))
		{
			temp36="";
		}
		if("".equals(des_pin.trim()))
		{
			temp52="";
		}
		if("".equals(bit23.trim()))
		{
			temp23="";
		}
		if("".equals(bit55.trim()))
		{
			temp55="";
		}
		if("".equals(bit14.trim()))
		{
			temp14="";
		}
		
		str="bit3,bit11"+temp14+",bit22"+temp23+",bit25,bit26"+temp35+temp36+",bit41,bit42,bit49"+temp52+",bit53"+temp55+",bit60,bit64";
		for (String one : str.split(",")) {
			Field filed = (Field) act.getBean(one);
			if (one.equals("bit2")) {
				filed.valueof(accountNumber);
			} else if (one.equals("bit3")) {
				filed.valueof("310000");
			} else if (one.equals("bit11")) {
				filed.valueof(serialNumber);
			} else if (one.equals("bit14")) {
				filed.valueof(bit14);
			} else if (one.equals("bit22")) {
				if("".equals(bit55.trim()))
				{
					filed.valueof("021");
				}else{
					filed.valueof("051");
				}
			} else if (one.equals("bit23")) {
				filed.valueof(bit23);
			} else if (one.equals("bit25")) {
				filed.valueof("00");
			} else if (one.equals("bit26")) {
				filed.valueof("12");
			} else if (one.equals("bit35")) {
				filed.valueof(trk2);
			} else if (one.equals("bit36")) {
				filed.valueof(trk3);
			} else if (one.equals("bit41")) {
				filed.valueof(this.PosNo);
			} else if (one.equals("bit42")) {
				filed.valueof(this.ShopCode);
			} else if (one.equals("bit49")) {
				filed.valueof("156");
			} else if (one.equals("bit52")) {
				byte[] tmp = PackageUtil.hexStringToByte(des_pin);
				filed.valueof(tmp);
			}else if (one.equals("bit53")) {
				filed.valueof("2600000000000000");
			}  else if (one.equals("bit55")) {
				filed.valueof(bit55);
			} else if (one.equals("bit60")) {
				if("".equals(bit55.trim()))
				{
					filed.valueof("01" + this.batch);
				}else{
					filed.valueof("01" + this.batch+"00050");
				}
			} else if (one.equals("bit64")) {
				byte[] tmp = PackageUtil.hexStringToByte("4632354542374636");
				filed.valueof(tmp);
			}
			pk.getData().put(filed.getFiledNumber(), filed);
		}

		this.bitmap = Package64.CreateBitMap(pk.getData().keySet());
		wf.log("[bitmap]=" + PackageUtil.byte2hex(this.bitmap),"调试");

		pk.setBitmap(this.bitmap);

		String tranMsg = PackageUtil.byte2hex(pk.getMessagetype())+ PackageUtil.byte2hex(pk.getBitmap());
		for (Integer one : pk.getData().keySet()) {
			if (one != 64) {
				String bitval = PackageUtil.byte2hex(pk.getData().get(one).toByte());
				tranMsg += bitval;
				wf.log("bit" + one + ":" + bitval, "调试");
			}
		}
		String macstr="";
		macstr=TestDes.MakeECBMac(MacKey, tranMsg,MasterKey);
		System.out.println("tranMsg:" + tranMsg);
		System.out.println("bit64:" + macstr);
		wf.log("bit64:" + macstr, "调试");

		Field filed = (Field) act.getBean("bit64");
		byte[] tmp = PackageUtil.hexStringToByte(macstr);
		filed.valueof(tmp);
		pk.getData().put(filed.getFiledNumber(), filed);
		byte[] pk_byteArray = pk.package64();
		ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
		System.out.println("pk_byteArray length: " + pk_byteArray.length);
		buff.putShort((short) pk_byteArray.length);
		buff.put(pk_byteArray);
		System.out.println(PackageUtil.byte2hex(buff.array()));
		wf.log(	"----------------------------------------------------", "调试");
		wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "	+ PackageUtil.byte2hex(buff.array()), "调试");
		wf.log("", "调试");
		QueryMsg = detection(buff.array());
		wf.log("响应包:长度 " + QueryMsg.length() / 2 + " 数据 "+ QueryMsg, "调试");
		if ((!QueryMsg.equals("-1")) && (!QueryMsg.equals(""))) {
			QueryMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(QueryMsg), "39,54",wf);
		} else {
			return "-1_";
		}
		wf.log("佘额查询返回数据:" + QueryMsg, "调试");
		return QueryMsg;
	}
	
	/**
	 * 帐单费用缴费（交易）
	 * @param trk2 二磁道数据
	 * @param trk3 三磁道数据
	 * @param pin 密码
	 * @param money 交易金额
	 * @param accountNumber 卡号
	 * @param serialNumber 订单号
	 * @param bit23 IC卡序列号
	 * @param bit55 IC卡 55 域
	 * @param bit14 IC卡 序列号
	 * @return 返回消费相关信息
	 * @throws Exception
	 */
	public String BillPayment(String trk2, String trk3, String pin,String money,String accountNumber,String serialNumber,String bit23,String bit55,String bit14) throws Exception {
//		System.out.println("$$$$$$$$$$$$$ trk2="+trk2+"$$$$$$$$$$$$$");
//		System.out.println("$$$$$$$$$$$$$ trk3="+trk3+"$$$$$$$$$$$$$");
//		System.out.println("$$$$$$$$$$$$$ Pin="+pin+"$$$$$$$$$$$$$");
//		System.out.println("$$$$$$$$$$$$$ money="+money+"$$$$$$$$$$$$$");
//		System.out.println("$$$$$$$$$$$$$ accountNumber="+accountNumber+"$$$$$$$$$$$$$");
//		System.out.println("$$$$$$$$$$$$$ serialNumber="+serialNumber+"$$$$$$$$$$$$$");
//		System.out.println("$$$$$$$$$$$$$ bit23="+bit23+"$$$$$$$$$$$$$");
//		System.out.println("$$$$$$$$$$$$$ bit55="+bit55+"$$$$$$$$$$$$$");
//		System.out.println("$$$$$$$$$$$$$ bit14="+bit14+"$$$$$$$$$$$$$");
//		
//		System.out.println("$$$$$$$$$$$$$ PinKey="+PinKey+"$$$$$$$$$$$$$");
//		System.out.println("$$$$$$$$$$$$$ MasterKey="+MasterKey+"$$$$$$$$$$$$$");
		
		String des_pin=TestDes.MakeDesPin(accountNumber, pin, PinKey,MasterKey);
		
		String QueryMsg = "";
		String TYPE_ID = "0200";
		Package64 pk = new Package64();
		pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
		pk.setHeader(PackageUtil.hexStringToByte(this.header));
		pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
		wf.log("-------------------请求包域值信息-------------------",	"调试");
		wf.log("[tpdu]=" + this.tpdu, "调试");
		wf.log("[header]=" + this.header, "调试");
		wf.log("[messagetype]=" + TYPE_ID, "调试");
		// 3、4、11、22、25、26、35、36、41、42、49、52、53、61、64
		String str = "bit3,bit4,bit11,bit22,bit25,bit26,bit35,bit36,bit41,bit42,bit49,bit52,bit53,bit61,bit64";
		String temp52=",bit52";
		String temp35 = ",bit35";
		String temp36 = ",bit36";
		String temp23=",bit23";
		String temp55=",bit55";
		String temp14=",bit14";
		if ("".equals(trk2.trim())) {
			temp35 = "";
		}
		if ("".equals(trk3.trim())) {
			temp36 = "";
		}
		if("".equals(des_pin.trim()))
		{
			temp52="";
		}
		if("".equals(bit23.trim()))
		{
			temp23="";
		}
		if("".equals(bit55.trim()))
		{
			temp55="";
		}
		if("".equals(bit14.trim()))
		{
			temp14="";
		}
		str="bit2,bit3,bit4,bit11"+temp14+",bit22"+temp23+",bit25,bit26"+temp35+temp36+",bit41,bit42,bit49"+temp52+",bit53"+temp55+",bit60,bit64";
		for (String one : str.split(",")) {
			Field filed = (Field) act.getBean(one);
			if (one.equals("bit2")) {
				filed.valueof(accountNumber);
			}else if (one.equals("bit3")) {
				filed.valueof("000000");
			} else if (one.equals("bit4")) {
				filed.valueof(money);
			} else if (one.equals("bit11")) {
				filed.valueof(serialNumber);
			} else if (one.equals("bit14")) {
				filed.valueof(bit14);
			} else if (one.equals("bit22")) {
				if("".equals(bit55.trim()))
				{
					filed.valueof("021");
				}else{
					filed.valueof("051");
				}
			} else if (one.equals("bit23")) {
				filed.valueof(bit23);
			} else if (one.equals("bit25")) {
				filed.valueof("00");
			} else if (one.equals("bit26")) {
				filed.valueof("12");
			} else if (one.equals("bit35")) {
				filed.valueof(trk2);
			} else if (one.equals("bit36")) {
				filed.valueof(trk3);
			} else if (one.equals("bit41")) {
				filed.valueof(this.PosNo);
			} else if (one.equals("bit42")) {
				filed.valueof(this.ShopCode);
			} else if (one.equals("bit49")) {
				filed.valueof("156");
			} else if (one.equals("bit52")) {
				byte[] tmp = PackageUtil.hexStringToByte(des_pin);
				filed.valueof(tmp);
			} else if (one.equals("bit53")) {
				filed.valueof("2600000000000000");
			} else if (one.equals("bit55")) {
				filed.valueof(bit55);
			} else if (one.equals("bit60")) {
				if("".equals(bit55.trim()))
				{
					filed.valueof("22" + this.batch);
				}else{
					filed.valueof("22" + this.batch+"00050");
				}
			} else if (one.equals("bit63")) {
				filed.valueof(PackageUtil.getCnASCII(this.operator));
			} else if (one.equals("bit64")) {
				byte[] tmp = PackageUtil.hexStringToByte("3335344331393934");
				filed.valueof(tmp);
			}
			pk.getData().put(filed.getFiledNumber(), filed);
		}
		pk.setBitmap(Package64.CreateBitMap(pk.getData().keySet()));

		String tranMsg = PackageUtil.byte2hex(pk.getMessagetype())
				+ PackageUtil.byte2hex(pk.getBitmap());
		for (Integer one : pk.getData().keySet()) {
			if (one != 64) {
				String bitval = PackageUtil.byte2hex(pk.getData().get(one).toByte());
				tranMsg += bitval;
				wf.log("bit" + one + ":" + bitval, "调试");
			}
		}
		// System.out.println("tranMsg:" + tranMsg);

		String macstr="";
		macstr=TestDes.MakeECBMac(MacKey, tranMsg,MasterKey);
		wf.log("bit64:" + macstr, "调试");
		// System.out.println("mac:" + macstr);
		Field filed = (Field) act.getBean("bit64");
		byte[] tmp = PackageUtil.hexStringToByte(macstr);
		filed.valueof(tmp);
		pk.getData().put(filed.getFiledNumber(), filed);

		byte[] pk_byteArray = pk.package64();
		ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
		// System.out.println("pk_byteArray length: " + pk_byteArray.length);
		buff.putShort((short) pk_byteArray.length);
		buff.put(pk_byteArray);
		// System.out.println(PackageUtil.byte2hex(buff.array()));

		wf.log("----------------------------------------------------", "调试");
		wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "+ PackageUtil.byte2hex(buff.array()), "调试");
		wf.log("", "调试");
		QueryMsg = detection(buff.array());
		wf.log("响应包:长度 " + QueryMsg.length() / 2 + " 数据 "+ QueryMsg, "调试");
		if ((!QueryMsg.equals("-1")) && (!QueryMsg.equals(""))) {
			QueryMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(QueryMsg),"2,4,11,12,13,14,37,38,39",wf);
		} else {
			return "-1_";
		}
		wf.log("帐单费用缴费返回数据:" + QueryMsg, "调试");
		return QueryMsg;
	}

	/**
	 * 帐单缴费冲正（交易）
	 * @param trk2 二磁道数据
	 * @param trk3 三磁道数据
	 * @param money 交易金额
	 * @param accountNumber 卡号
	 * @param serialNumber 交易流水
	 * @param bit37 bit37域数据  参考号    
	 * @param bit38 bit38域数据  授权码    
	 * @param bit23 IC卡序列号
	 * @param bit55 IC卡 55 域
	 * @param bit14 IC卡 序列号
	 * @return 订单信息
	 * @throws Exception
	 */
	public String unBillPayment(String trk2, String trk3,String money,String accountNumber,String serialNumber,String bit37,String bit38,String bit23,String bit55,String bit14) throws Exception {
		String QueryMsg = "";
		String TYPE_ID = "0400";
		//this.serialNumber = unAddSerialNumber(this.serialNumber);
		Package64 pk = new Package64();
		pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
		pk.setHeader(PackageUtil.hexStringToByte(this.header));
		pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
		wf.log("-------------------请求包域值信息-------------------","调试");
		wf.log("[tpdu]=" + this.tpdu, "调试");
		wf.log("[header]=" + this.header, "调试");
		wf.log("[messagetype]=" + TYPE_ID, "调试");
		// 冲正39域为冲正原因,取值如下:
		/*
		 * 98 POS终端在时限内未能收到POS中心的应答 96 POS终端收到POS中心的批准应答消息，但由于POS机故障无法完成交易 A0
		 * POS终端对收到POS中心的批准应答消息，验证MAC出错 06 其他情况引发的冲正
		 */
		String str = "bit3,bit4,bit11,bit22,bit25,bit35,bit36,bit38,bit39,bit41,bit42,bit49,bit61,bit62,bit64";
//		String temp35=",bit35";
//		String temp36=",bit36";
//		String temp37 = ",bit37";
		String temp38 = ",bit38";
		
		String temp23 = ",bit23";
		String temp55 = ",bit55";
		String temp14 = ",bit14";
//		if("".equals(trk2))
//		{
//			temp35="";
//		}
//		if("".equals(trk3))
//		{
//			temp36="";
//		}
//		if("".equals(bit37))
//		{
//			temp37="";
//		}
		if("".equals(bit38))
		{
			temp38="";
		}
		if("".equals(bit23))
		{
			temp23="";
		}
		if("".equals(bit55))
		{
			temp55="";
		}
		if("".equals(bit14))
		{
			temp14="";
		}
		str="bit3,bit4,bit11"+temp14+",bit22"+temp23+",bit25"+temp38+",bit39,bit41,bit42,bit49"+temp55+",bit60,bit64";
		for (String one : str.split(",")) {
			Field filed = (Field) act.getBean(one);
			if (one.equals("bit2")) {
				filed.valueof(accountNumber);
			} else if (one.equals("bit3")) {
				filed.valueof("000000");
			} else if (one.equals("bit4")) {
				filed.valueof(money);
			} else if (one.equals("bit11")) {
				filed.valueof(serialNumber);
			} else if (one.equals("bit14")) {
				filed.valueof(bit14);
			} else if (one.equals("bit22")) {
				if("".equals(bit55.trim()))
				{
					filed.valueof("021");
				}else{
					filed.valueof("051");
				}
			} else if (one.equals("bit23")) {
				filed.valueof(bit23);
			} else if (one.equals("bit25")) {
				filed.valueof("00");
			} else if (one.equals("bit35")) {
				filed.valueof(trk2);
			} else if (one.equals("bit36")) {
				filed.valueof(trk3);
			} else if (one.equals("bit37")) {
				filed.valueof(bit37);
			} else if (one.equals("bit38")) {
				filed.valueof(bit38);
			} else if (one.equals("bit39")) {
				filed.valueof("06");
			} else if (one.equals("bit41")) {
				filed.valueof(this.PosNo);
			} else if (one.equals("bit42")) {
				filed.valueof(this.ShopCode);
			} else if (one.equals("bit49")) {
				filed.valueof("156");
			} else if (one.equals("bit55")) {
				filed.valueof(bit55);
			} else if (one.equals("bit60")) {
				if("".equals(bit55.trim()))
				{
					filed.valueof("22" + this.batch);
				}else{
					filed.valueof("22" + this.batch+"00050");
				}
			} else if (one.equals("bit63")) {
				filed.valueof(PackageUtil.getCnASCII(this.operator));
			} else if (one.equals("bit64")) {
				byte[] tmp = PackageUtil.hexStringToByte("0000000000000000");
				filed.valueof(tmp);
			}
			pk.getData().put(filed.getFiledNumber(), filed);
		}
		pk.setBitmap(Package64.CreateBitMap(pk.getData().keySet()));

		String tranMsg = PackageUtil.byte2hex(pk.getMessagetype())
				+ PackageUtil.byte2hex(pk.getBitmap());
		for (Integer one : pk.getData().keySet()) {
			if (one != 64) {
				String bitval = PackageUtil.byte2hex(pk.getData().get(one)
						.toByte());
				tranMsg += bitval;
				wf.log("bit" + one + ":" + bitval, "调试");
			}
		}
		System.out.println("tranMsg:" + tranMsg);

		String macstr="";
		macstr=TestDes.MakeECBMac(MacKey, tranMsg,MasterKey);
		wf.log("bit64:" + macstr, "调试");
		// System.out.println("mac:" + macstr);
		Field filed = (Field) act.getBean("bit64");
		byte[] tmp = PackageUtil.hexStringToByte(macstr);
		filed.valueof(tmp);
		pk.getData().put(filed.getFiledNumber(), filed);

		byte[] pk_byteArray = pk.package64();
		ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
		// System.out.println("pk_byteArray length: " + pk_byteArray.length);
		buff.putShort((short) pk_byteArray.length);
		buff.put(pk_byteArray);
		// System.out.println(PackageUtil.byte2hex(buff.array()));
		wf.log(				"----------------------------------------------------", "调试");
		wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "	+ PackageUtil.byte2hex(buff.array()), "调试");
		wf.log("", "调试");
		
		QueryMsg = detection(buff.array());

		wf.log("响应包:长度 " + QueryMsg.length() / 2 + " 数据 "+ QueryMsg, "调试");
		if ((!QueryMsg.equals("-1")) && (!QueryMsg.equals(""))) {
			QueryMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(QueryMsg),"2,4,11,12,13,14,37,38,39,44,61",wf);
		} else {
			return "-1_";
		}
		wf.log("帐单缴费冲正返回数据:" + QueryMsg, "调试");
		return QueryMsg;
	}
	/**
	 * 帐单缴费冲正（交易）
	 * @param trk2 二磁道数据
	 * @param trk3 三磁道数据
	 * @param pin 卡交易密码
	 * @param money 交易金额
	 * @param accountNumber 卡号
	 * @param serialNumber 交易流水
	 * @param bit37 bit37域数据   参考号   
	 * @param bit38 bit38域数据   授权码
	 * @param serialNumber2 原订单交易流水
	 * @param bit23 IC卡序列号
	 * @param bit55 IC卡 55 域
	 * @param bit14 IC卡 序列号
	 * @return 订单信息
	 * @throws Exception
	 */
	public String undoBillPayment(String trk2, String trk3, String pin,String money,String accountNumber,String serialNumber,String bit37,String bit38,String serialNumber2,String bit23,String bit55,String bit14) throws Exception {
		String des_pin=TestDes.MakeDesPin(accountNumber, pin, PinKey,MasterKey);
		
		String QueryMsg = "";
		String TYPE_ID = "0200";
		//this.serialNumber = unAddSerialNumber(this.serialNumber);
		Package64 pk = new Package64();
		pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
		pk.setHeader(PackageUtil.hexStringToByte(""));
		pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
		wf.log("-------------------请求包域值信息-------------------","调试");
		wf.log("[tpdu]=" + this.tpdu, "调试");
		wf.log("[header]=" + this.header, "调试");
		wf.log("[messagetype]=" + TYPE_ID, "调试");
		// 冲正39域为冲正原因,取值如下:
		/*
		 * 98 POS终端在时限内未能收到POS中心的应答 96 POS终端收到POS中心的批准应答消息，但由于POS机故障无法完成交易 A0
		 * POS终端对收到POS中心的批准应答消息，验证MAC出错 06 其他情况引发的冲正
		 */
		String str = "bit3,bit4,bit11,bit22,bit25,bit26,bit35,bit36,bit38,bit39,bit41,bit42,bit49,bit61,bit62,bit64";
		String temp35=",bit35";
		String temp36=",bit36";
		String temp37 = ",bit37";
		String temp38 = ",bit38";
		
		String temp23 = ",bit23";
		String temp55 = ",bit55";
		String temp14 = ",bit14";
		if("".equals(trk2))
		{
			temp35="";
		}
		if("".equals(trk3))
		{
			temp36="";
		}
		if("".equals(bit37))
		{
			temp37="";
		}
		if("".equals(bit38))
		{
			temp38="";
		}
		if("".equals(bit23))
		{
			temp23="";
		}
		if("".equals(bit55))
		{
			temp55="";
		}
		if("".equals(bit14))
		{
			temp14="";
		}
		str="bit2,bit3,bit4,bit11"+temp14+",bit22"+temp23+",bit25,bit26"+temp35+temp36+temp37+temp38+",bit41,bit42,bit49,bit52,bit53"+temp55+",bit60,bit61,bit63,bit64";
		for (String one : str.split(",")) {
			Field filed = (Field) act.getBean(one);
			if (one.equals("bit2")) {
				filed.valueof(accountNumber);
			}else if (one.equals("bit3")) {
				filed.valueof("200000");
			} else if (one.equals("bit4")) {
				filed.valueof(money);
			} else if (one.equals("bit11")) {
				filed.valueof(serialNumber);
			} else if (one.equals("bit14")) {
				filed.valueof(bit14);
			} else if (one.equals("bit22")) {
				if("".equals(bit55.trim()))
				{
					filed.valueof("021");
				}else{
					filed.valueof("051");
				}
			} else if (one.equals("bit23")) {
				filed.valueof(bit23);
			} else if (one.equals("bit25")) {
				filed.valueof("00");
			} else if (one.equals("bit26")) {
				filed.valueof("06");
			} else if (one.equals("bit35")) {
				filed.valueof(trk2);
			} else if (one.equals("bit36")) {
				filed.valueof(trk3);
			} else if (one.equals("bit37")) {
				filed.valueof(bit37);
			} else if (one.equals("bit38")) {
				filed.valueof(bit38);
			} else if (one.equals("bit41")) {
				filed.valueof(this.PosNo);
			} else if (one.equals("bit42")) {
				filed.valueof(this.ShopCode);
			} else if (one.equals("bit49")) {
				filed.valueof("156");
			} else if (one.equals("bit52")) {
				byte[] tmp = PackageUtil.hexStringToByte(des_pin);
				filed.valueof(tmp);
			} else if (one.equals("bit53")) {
				filed.valueof("2600000000000000");
			} else if (one.equals("bit55")) {
				filed.valueof(bit55);
			} else if (one.equals("bit60")) {
				if("".equals(bit55.trim()))
				{
					filed.valueof("23" + this.batch);
				}else{
					filed.valueof("23" + this.batch+"00050");
				}
			} else if (one.equals("bit61")) {
				filed.valueof(this.batch+serialNumber2);
			} else if (one.equals("bit63")) {
				filed.valueof(PackageUtil.getCnASCII(this.operator));
			}  else if (one.equals("bit64")) {
				byte[] tmp = PackageUtil.hexStringToByte("0000000000000000");
				filed.valueof(tmp);
			}
			pk.getData().put(filed.getFiledNumber(), filed);
		}
		pk.setBitmap(Package64.CreateBitMap(pk.getData().keySet()));

		String tranMsg = PackageUtil.byte2hex(pk.getMessagetype())
				+ PackageUtil.byte2hex(pk.getBitmap());
		for (Integer one : pk.getData().keySet()) {
			if (one != 64) {
				String bitval = PackageUtil.byte2hex(pk.getData().get(one)
						.toByte());
				tranMsg += bitval;
				wf.log("bit" + one + ":" + bitval, "调试");
			}
		}
		System.out.println("tranMsg:" + tranMsg);

		String macstr="";
		macstr=TestDes.MakeECBMac(MacKey, tranMsg,MasterKey);
		wf.log("bit64:" + macstr, "调试");
		// System.out.println("mac:" + macstr);
		Field filed = (Field) act.getBean("bit64");
		byte[] tmp = PackageUtil.hexStringToByte(macstr);
		filed.valueof(tmp);
		pk.getData().put(filed.getFiledNumber(), filed);

		byte[] pk_byteArray = pk.package64();
		ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
		// System.out.println("pk_byteArray length: " + pk_byteArray.length);
		buff.putShort((short) pk_byteArray.length);
		buff.put(pk_byteArray);
		// System.out.println(PackageUtil.byte2hex(buff.array()));
		wf.log(				"----------------------------------------------------", "调试");
		wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "	+ PackageUtil.byte2hex(buff.array()), "调试");
		wf.log("", "调试");
		
		QueryMsg = detection(buff.array());
		if ((!QueryMsg.equals("-1")) && (!QueryMsg.equals("")))
		{
			String tpdu=QueryMsg.substring(0, 10);
			String Msg=QueryMsg.substring(10);
			QueryMsg=tpdu+header+Msg;
		}
		wf.log("响应包:长度 " + QueryMsg.length() / 2 + " 数据 "+ QueryMsg, "调试");
		if ((!QueryMsg.equals("-1")) && (!QueryMsg.equals(""))) {
			QueryMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(QueryMsg),"2,4,11,12,13,14,37,38,39,44,61",wf);
		} else {
			return "-1_";
		}
		wf.log("帐单缴费冲正返回数据:" + QueryMsg, "调试");
		return QueryMsg;
	}
	/**
	 * IC卡传输密钥下载
	 * 
	 * @return 工作密钥
	 * @throws Exception
	 */
	public String ICTransKeyDown(String ICID,String ICNo) throws Exception {
		String LoginMsg = "0";
		String TYPE_ID = "0800";
		wf.log("-------------------请求包域值信息-------------------",	"调试");
		Package64 pk = new Package64();
		pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
		pk.setHeader(PackageUtil.hexStringToByte(this.header));
		pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
		wf.log("[tpdu]=" + this.tpdu, "调试");
		wf.log("[header]=" + this.header, "调试");
		wf.log("[messagetype]=" + TYPE_ID, "调试");
		// 11、41、42、60、63
		String str = "bit3,bit34";
		for (String one : str.split(",")) {
			Field filed = (Field) act.getBean(one);
			if (one.equals("bit3")) {
				filed.valueof("900000");
			} else if (one.equals("bit34")) {
				filed.valueof(ICID + ICNo);
			}
			pk.getData().put(filed.getFiledNumber(), filed);
		}
		this.bitmap = Package64.CreateBitMap(pk.getData().keySet());
		wf.log("[bitmap]=" + PackageUtil.byte2hex(this.bitmap),"调试");
		for (Integer one : pk.getData().keySet()) {
			wf.log("bit" + one + ":"+ PackageUtil.byte2hex(pk.getData().get(one).toByte()),"调试");
		}
		wf.log("----------------------------------------------------", "调试");
		pk.setBitmap(this.bitmap);
		byte[] pk_byteArray = pk.package64();
		ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
		buff.putShort((short) pk_byteArray.length);
		buff.put(pk_byteArray);


		wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "	+ PackageUtil.byte2hex(buff.array()), "调试");
		wf.log("", "调试");
		LoginMsg = detection(buff.array());
		wf.log("响应包:长度 " + LoginMsg.length() / 2 + " 数据 "+ LoginMsg, "调试");
		if ((!LoginMsg.equals("-1")) && (!LoginMsg.equals(""))) {
			LoginMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(LoginMsg), "39,62",wf);
		} else {
			return "-1_";
		}
		wf.log("银联签到返回数据:" + LoginMsg, "调试");
		return LoginMsg;
	}
	/**
	 * POS状态上送
	 * 
	 * @param	ParmType
	 * 				参数下载类别
	 * 					磁条卡交易采用362；
	 * 					IC卡公钥下载交易采用372；
	 * 					IC卡参数下载交易采用382
	 * 
	 * @return 批次号
	 * @throws Exception
	 */
	public String POSStatuUP(String ParmType) throws Exception {
		String LoginMsg = "0";
		String TYPE_ID = "0820";
		wf.log("-------------------请求包域值信息-------------------",	"调试");
		Package64 pk = new Package64();
		pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
		pk.setHeader(PackageUtil.hexStringToByte(this.header));
		pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
		wf.log("[tpdu]=" + this.tpdu, "调试");
		wf.log("[header]=" + this.header, "调试");
		wf.log("[messagetype]=" + TYPE_ID, "调试");
		// 11、41、42、60、63
		String str = "bit41,bit42,bit60,bit62";
		for (String one : str.split(",")) {
			Field filed = (Field) act.getBean(one);
			if (one.equals("bit41")) {
				filed.valueof(this.PosNo);
			}else if (one.equals("bit42")) {
				filed.valueof(this.ShopCode);
			} else if (one.equals("bit60")) {
				filed.valueof("00" + this.batch + ParmType);
			} else if (one.equals("bit62")) {
				filed.valueof(PackageUtil.getCnASCII("100"));
			} 
			pk.getData().put(filed.getFiledNumber(), filed);
		}
		this.bitmap = Package64.CreateBitMap(pk.getData().keySet());
		wf.log("[bitmap]=" + PackageUtil.byte2hex(this.bitmap),"调试");
		for (Integer one : pk.getData().keySet()) {
			wf.log("bit" + one + ":"+ PackageUtil.byte2hex(pk.getData().get(one).toByte()),"调试");
		}
		wf.log("----------------------------------------------------", "调试");
		pk.setBitmap(this.bitmap);
		byte[] pk_byteArray = pk.package64();
		ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
		buff.putShort((short) pk_byteArray.length);
		buff.put(pk_byteArray);


		wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "	+ PackageUtil.byte2hex(buff.array()), "调试");
		wf.log("", "调试");
		LoginMsg = detection(buff.array());
		wf.log("响应包:长度 " + LoginMsg.length() / 2 + " 数据 "+ LoginMsg, "调试");
		if ((!LoginMsg.equals("-1")) && (!LoginMsg.equals(""))) {
			LoginMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(LoginMsg), "39,62",wf);
		} else {
			return "-1_";
		}
		wf.log("银联签到返回数据:" + LoginMsg, "调试");
		return LoginMsg;
	}
	/**
	 * POS参数传递
	 * 
	 * @param	ParmType
	 * 				参数下载类别
	 *					磁条卡交易采用360/361
	 * 					IC卡公钥下载交易采用370/371;
	 *					IC卡参数下载交易采用380/381;
	 *					TMS参数下载结束采用364/365;
	 *					卡BIN黑名单下载结束采用390/391
	 * @return 批次号
	 * @throws Exception
	 */
	public String POSICParmKeyDown(String ParmType,String RIDParm) throws Exception {
		String LoginMsg = "0";
		String TYPE_ID = "0800";
		wf.log("-------------------请求包域值信息-------------------",	"调试");
		Package64 pk = new Package64();
		pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
		pk.setHeader(PackageUtil.hexStringToByte(this.header));
		pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
		wf.log("[tpdu]=" + this.tpdu, "调试");
		wf.log("[header]=" + this.header, "调试");
		wf.log("[messagetype]=" + TYPE_ID, "调试");
		// 11、41、42、60、63
		String str = "bit41,bit42,bit60,bit62";
		for (String one : str.split(",")) {
			Field filed = (Field) act.getBean(one);
			if (one.equals("bit41")) {
				filed.valueof(this.PosNo);
			}else if (one.equals("bit42")) {
				filed.valueof(this.ShopCode);
			} else if (one.equals("bit60")) {
				filed.valueof("00" + this.batch + ParmType);
			} else if (one.equals("bit62")) {
				filed.valueof(RIDParm);
			} 
			pk.getData().put(filed.getFiledNumber(), filed);
		}
		this.bitmap = Package64.CreateBitMap(pk.getData().keySet());
		wf.log("[bitmap]=" + PackageUtil.byte2hex(this.bitmap),"调试");
		for (Integer one : pk.getData().keySet()) {
			wf.log("bit" + one + ":"+ PackageUtil.byte2hex(pk.getData().get(one).toByte()),"调试");
		}
		wf.log("----------------------------------------------------", "调试");
		pk.setBitmap(this.bitmap);
		byte[] pk_byteArray = pk.package64();
		ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
		buff.putShort((short) pk_byteArray.length);
		buff.put(pk_byteArray);


		wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "	+ PackageUtil.byte2hex(buff.array()), "调试");
		wf.log("", "调试");
		LoginMsg = detection(buff.array());
		wf.log("响应包:长度 " + LoginMsg.length() / 2 + " 数据 "+ LoginMsg, "调试");
		if ((!LoginMsg.equals("-1")) && (!LoginMsg.equals(""))) {
			LoginMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(LoginMsg), "39,62",wf);
		} else {
			return "-1_";
		}
		wf.log("银联签到返回数据:" + LoginMsg, "调试");
		return LoginMsg;
	}
	
	//计算秘钥
	public static void  getMastKey() throws Exception{
		String fl1 = "710E2111C9DCB2E8DA207B32FD88416D";//分量1
		String fl2 = "54EE5064814D11C9AD86D905912A1104";//分量2
		String key = DesUtil.byte2hex(TestDes.XORContent(DesUtil.hexStringToByte(fl1), DesUtil.hexStringToByte(fl2)));
		System.out.println(key);
//		String MasterKey=DesUtil.decode3DES("16C346D4749B97D233078C928CB06F67","25E071754891A32177A6A2376CA25069");
//		String MasterKey1=DesUtil.decode3DES("BC317706FD70751AFE42CA5080A9F6D5","25E071754891A32177A6A2376CA25069");
//		System.out.println(MasterKey);
//		System.out.println(MasterKey1);
	}
	
	
//	public static void main(String[] args) throws Exception {
//		//000013_75F4F30ED209B0A4012E44BC7387F6D3_B678BCBA75F4F30ED209B0A400000000
//		//测试参数
////		ipAdd="112.5.145.88";
////		IPort="9906";
////		String ShopCode = "Z07000000141863";//商户号
////		String PosNo = "30164177";			//终端号
////		String batch = "000013"; 
////		String MasterKey="74757105913910210314678310910894";
////		String PinKey="73B1F119279397D8E7221108D3F1C5C2";
////		String MacKey="73B1F119279397D873B1F119279397D8";
//		
//		//生产参数 费率
//		ipAdd="112.5.192.232";//
//		IPort="9443";
//		String ShopCode = "210002205001002";//商户号
//		String PosNo = "20001084";			//终端号
//		String batch = "000001"; 
//		String MasterKey="3267EADC98E3B04C015254A81C9807A4";//0_00_000001_588E0AE95BD46C27CE5AF3DEFBAE7DF9_242912CBCD754229242912CBCD754229
//		String PinKey="588E0AE95BD46C27CE5AF3DEFBAE7DF9";
//		String MacKey="242912CBCD754229242912CBCD754229";
//		
////		String ShopCode = "310003903001830";//商户号
////		String PosNo = "90001964";			//终端号
////		String batch = "000001"; 
////		String MasterKey="1A9E9DFD868A7323C1FE70D64F73E30B";
////		String PinKey="C05F898AD73CF21A62FE8ECB3FC6A255";
////		String MacKey="B96A917062D0FB08B96A917062D0FB08";
//		
//		
//		RuiShengGTSocketToBoss st = new RuiShengGTSocketToBoss(ShopCode,PosNo,batch,MasterKey,PinKey,MacKey);
//		String serialNumber = "000012";
//		//String trk2 = "6259960005135454d21032011442713500000";
//		
//		//trk2="6217900100000371909d22092201000065500";
////		String accountNumber = "6210300016512078";
////		String trk2="6210300016512078d49121206307260000000"; //北京银行
////		String Pin="147258";
//		String accountNumber = "6259960005135454";
//		String trk2="6259960005135454d21032011442713500000"; //北京银行
//		String Pin="147258";
//		
//		String trk3 = "";
//		//String des_pin=TestDes.MakeDesPin(trk2.split("d")[0], Pin, st.PinKey,st.MasterKey);
//		//System.out.println("des_pin:"+des_pin);
//		//交易金额
//		String money = "000000000001";
//		String bit37="061608280622";
//		String bit38="019695";
//		String serialNumber2="900030";
//		//IC卡的ID号(32长度,长度不足后补空格)
//		String ICID="";
//		//IC卡编号(4长度,长度不足前补0)
//		String ICNo="";
//		//参数下载类别
//		//		磁条卡交易采用360/361
//		// 		IC卡公钥下载交易采用370/371;
//		//		IC卡参数下载交易采用380/381;
//		//		TMS参数下载结束采用364/365;
//		//		卡BIN黑名单下载结束采用390/391
//		String ParmType="370";
//		String bit23="";
//		String bit55="";
//		String bit14="";
//		//st.serialNumber="200028";
//		//String masKey="AA17070598B13C472F4C502D13F4D6CB";
//		//masKey=DesUtil.decode3DES(masKey,st.MPOSKey);
//		//System.out.println("masKey:"+masKey);
////		System.out.println("终端签到,返回:" + st.Login0800(serialNumber));
////		System.out.println("银行卡余额查询,返回:" + st.Query(trk2, trk3, Pin,accountNumber,serialNumber,bit23,bit55,bit14));
//		
////		System.out.println("消费,返回:" + st.BillPayment(trk2, trk3,  Pin, money,accountNumber,serialNumber, bit23, bit55, bit14));
//
//		//消费,返回:0_6259960005135454_000000000001_000012_153120_0616_ _061608280622_019695_00
////		System.out.println("冲正,返回:" +st.unBillPayment(trk2, trk3,money,accountNumber,serialNumber, bit37, bit38,bit23,bit55,bit14));	
////		System.out.println("消费撤消,返回:"+st.undoBillPayment(trk2, trk3, Pin,accountNumber,serialNumber, money, bit37, bit38, serialNumber2,bit23,bit55,bit14));
//		//System.out.println("IC卡传输密钥下载,返回:"+st.ICTransKeyDown(ICID, ICNo));
//		//需要下载的公钥RID和索引，采用TLV格式，每次只能请求一组公钥
//		//String RIDParm="9F0605A0000003339F220108";
//		//System.out.println("POS参数下载,返回:"+st.POSICParmKeyDown(ParmType,RIDParm));
//		
//		//磁条卡交易采用362；
//		//IC卡公钥下载交易采用372；
//		//IC卡参数下载交易采用382
//		String ParmType2="382";
//		//System.out.println("POS状态上送,返回:"+st.POSStatuUP(ParmType2));
//		
////		getMastKey();
//	}
}